Skip to content

Add MsalRemainingTokenLifetime histogram for token expiry #5920

Open
neha-bhargava wants to merge 3 commits intomainfrom
nebharg/otelRemainingTokenLife
Open

Add MsalRemainingTokenLifetime histogram for token expiry #5920
neha-bhargava wants to merge 3 commits intomainfrom
nebharg/otelRemainingTokenLife

Conversation

@neha-bhargava
Copy link
Copy Markdown
Contributor

@neha-bhargava neha-bhargava commented Apr 10, 2026

Add new OTel histogram (MsalRemainingTokenLifetime.1A) that records the remaining lifetime of tokens in seconds at the time of acquisition.

Tags (6/8): MsalVersionPlatform (combined), ApiId, TokenSource, CacheLevel, CacheRefreshReason, TokenType.

Also adds TelemetryTokenTypeConstants.ToDisplayString() helper for int-to- string token type mapping used by the new histogram.

Resolves: #5889
PBI: 3554274

Fixes #

Changes proposed in this request

Testing

Performance impact

Documentation

  • All relevant documentation is updated.

…vability

Add new OTel histogram (MsalRemainingTokenLifetime.1A) that records the
remaining lifetime of tokens in minutes at the time of acquisition.

Tags (6/8): MsalVersionPlatform (combined), ApiId, TokenSource, CacheLevel,
CacheRefreshReason, TokenType (human-readable string).

Also adds TelemetryTokenTypeConstants.ToDisplayString() helper for int-to-
string token type mapping used by the new histogram.

Resolves: #5889
PBI: 3554274

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@neha-bhargava neha-bhargava requested a review from a team as a code owner April 10, 2026 22:44
Copilot AI review requested due to automatic review settings April 10, 2026 22:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new OpenTelemetry histogram (MsalRemainingTokenLifetime.1A) to capture token time-to-expiry at acquisition time, and wires it into the successful token acquisition telemetry path. This supports the requested MSAL telemetry enhancement for remaining token lifetime.

Changes:

  • Added MsalRemainingTokenLifetime.1A histogram emission (with tags) in OtelInstrumentation and invoked it from the success telemetry path.
  • Added TelemetryTokenTypeConstants.ToDisplayString(int) to emit a human-readable token type for the new metric.
  • Updated OTel unit tests to account for the additional exported metric and validate tags for the new histogram.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/Microsoft.Identity.Test.Unit/TelemetryTests/OTelInstrumentationTests.cs Updates expected metric counts and adds tag validation for the new remaining-lifetime histogram.
src/client/Microsoft.Identity.Client/TelemetryCore/TelemetryConstants.cs Adds MsalVersionPlatform tag key constant used by the new metric.
src/client/Microsoft.Identity.Client/TelemetryCore/OpenTelemetry/IOtelInstrumentation.cs Extends the instrumentation interface with LogRemainingTokenLifetime(...).
src/client/Microsoft.Identity.Client/TelemetryCore/OpenTelemetry/NullOtelInstrumentation.cs Implements the new interface method as a no-op for null instrumentation.
src/client/Microsoft.Identity.Client/Platforms/Features/OpenTelemetry/OtelInstrumentation.cs Defines and records the new MsalRemainingTokenLifetime.1A histogram.
src/client/Microsoft.Identity.Client/Internal/TelemetryTokenTypeConstants.cs Adds an int→string token type mapping helper for telemetry display.
src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs Calls LogRemainingTokenLifetime(...) during successful request telemetry logging.

Comment thread src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs Outdated
Comment thread src/client/Microsoft.Identity.Client/Internal/TelemetryTokenTypeConstants.cs Outdated
expectedTags.Add(TelemetryConstants.ApiId);
expectedTags.Add(TelemetryConstants.TokenSource);
expectedTags.Add(TelemetryConstants.CacheLevel);
expectedTags.Add(TelemetryConstants.CacheRefreshReason);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test validates tags but never asserts the recorded histogram value

Copy link
Copy Markdown
Contributor

@Robbie-Microsoft Robbie-Microsoft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description says tags are "6/8" but only 6 are emitted. The description mentions 8 planned tags - it's unclear which 2 are missing and whether that's intentional or an oversight worth clarifying.

neha-bhargava and others added 2 commits April 29, 2026 14:50
- Merge LogRemainingTokenLifetime into LogSuccessMetrics (ssmelov comment)
- Remove duplicate GetCacheLevel call, compute once and reuse (Robbie +1)
- Use int for TokenType tag instead of string for consistency with other metrics
- Remove ToDisplayString helper (no longer needed with int TokenType)
- Add histogram value assertions in test (count > 0, sum >= 0)
- XML doc correctly says seconds (align PR description accordingly)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The new MsalRemainingTokenLifetime histogram adds one more metric to
the export, bringing the total from 5 to 6.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 29, 2026 22:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Comment on lines +92 to +98
/// <summary>
/// Histogram to record the remaining lifetime of acquired tokens in seconds.
/// </summary>
internal static readonly Lazy<Histogram<long>> s_remainingTokenLifetime = new(() => Meter.CreateHistogram<long>(
RemainingTokenLifetimeHistogramName,
unit: "s",
description: "Remaining lifetime of acquired tokens at the time of acquisition."));
Copy link

Copilot AI Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description says MsalRemainingTokenLifetime.1A should record remaining token lifetime in minutes, but the instrument is defined/recorded in seconds (unit "s" and TotalSeconds). Either update the implementation/unit to minutes, or update the PR description and any downstream expectations to explicitly be seconds.

Copilot uses AI. Check for mistakes.
Comment on lines +189 to +195
s_remainingTokenLifetime.Value.Record(remainingSeconds,
new(TelemetryConstants.MsalVersionPlatform, $"{MsalIdHelper.GetMsalVersion()},{platform}"),
new(TelemetryConstants.ApiId, apiId),
new(TelemetryConstants.TokenSource, authResultMetadata.TokenSource),
new(TelemetryConstants.CacheLevel, cacheLevel),
new(TelemetryConstants.CacheRefreshReason, authResultMetadata.CacheRefreshReason),
new(TelemetryConstants.TokenType, authResultMetadata.TelemetryTokenType));
Copy link

Copilot AI Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description calls out TokenType tag as a human-readable string and mentions adding a TelemetryTokenTypeConstants.ToDisplayString() helper, but this metric records authResultMetadata.TelemetryTokenType (an int). If TokenType is intended to be a display string for this histogram, convert the int to the display string (e.g., via the helper) before tagging; otherwise, update the PR description to match the emitted tag value type.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] MSAL telemetry enhancement

4 participants